package com.irononetech.android.MultilevelListView;

import com.irononetech.android.Application.Application;
import com.irononetech.android.formcomponent.FormObject;
import com.irononetech.android.formcomponent.view.VehicleDetailsActivity;
import com.irononetech.android.claimsonersa.R;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ExpList3_ThreeWheel extends ExpandableListActivity{

	Logger LOG = LoggerFactory.getLogger(ExpList3_ThreeWheel.class);

	
	static final String listdesc[][][][] = 
	{
    	{ // grey
    		{
    		{ "ThreeWheel", "All parts" },
  		  	{ "Headlight Lh","" },
  			{ "Headlight Rh","" },
  			{ "Headlight cowl","" },
  			{ "Signal light Rh","" },
  			{ "Signal Light Lh","" },
  			{ "Windscreen Frame","" },
  			{ "Windscreen","" },
  			{ "Windscreen Beading","" },
  			{ "Joint Beading","" },
  			{ "Front Fender","" },
  			{ "Fork","" },
  			{ "Pin Set","" },
  			{ "Cup Set","" },
  			{ "Ball Set","" },
  			{ "Hood Canopy","" },
  			{ "Front Frame","" },
  			{ "Rear Frame set","" },
  			{ "Rear Rh Fender","" },
  			{ "Rear Lh Fender","" },
  			{ "Rear Buffer","" },
  			{ "Rear Rh Combination Lamp","" },
  			{ "Rear Lh Combination Lamp","" },
  			{ "Rear Door","" },
  			{ "Side Door","" },
  			{ "Side Plastic LH","" },
  			{ "Side Plastic RH","" },
  			{ "Kick Pedal","" },
  			{ "Cabin Hole","" },
  			{ "Handle","" },
  			{ "Swing Arm","" },
  			{ "Engine Housing","" },
  			{ "Battery","" },
  			{ "Petrol Tank","" },
  			{ "Rear Deck","" },
  			{ "Cable set","" },
  			{ "Horn Guard","" },
  			{ "Side Mirror Rh",""  },
 			{ "Side Mirror Lh","" },
 			{ "Front Fork","" },
 			{ "Front Hub","" },
 			{ "Front Axle","" },
 			{ "Fork Pin","" },
 			{ "Cup set Upper/Lower","" },
 			{ "Handle Ball","" },
 			{ "Meter","" },
 			{ "Meter Cable","" },
 			{ "Gear Cable Black","" },
 			{ "Gear Cable White","" },
 			{ "F shock absorber","" },
 			{ "Clutch cable","" },
 			{ "Chock Cable","" },
 			{ "Front Rim","" },
 			{ "Front Tire","" },
 			{ "Front Mud Guard","" },
 			{ "F/Windscreen","" },
 			{ "F/W/LH Plastic","" },
 			{ "F/W/RH Plastic","" },
 			{ "F/ Windscreen Beading","" },
 			{ "F/W/B Clip","" },
 			{ "RH Mirror","" },
 			{ "LH Mirror","" },
 			{ "Dash Board","" },
 			{ "F/Brake Hose","" },
 			{ "F/Brake Pipe","" },
 			{ "Brake Pump","" },
 			{ "Hood Cloth","" },
 			{ "Hood Pipe","" },
 			{ "Hood Frame","" },
 			{ "Front Seat","" },
 			{ "Front seat Frame","" },
 			{ "Head Light","" },
 			{ "Head Light Cover RH","" }, 
 			{ "Head Light Cover LH","" },
 			{ "Front LH Signal Light","" },
 			{ "Front RH signal Light","" },
 			{ "Wiper Blade","" },
 			{ "Wiper Motor","" },
 			{ "R/LH Mud Guard","" },
 			{ "R/RH Mud Guard","" },
 			{ "R/Seat","" },
 			{ "R/Buffer","" },
 			{ "R/Door","" },
 			{ "Reverse Gear Cable","" },
 			{ "RH Door","" },
 			{ "R/RH Shock absorber","" },
 			{ "R/LH Shock absorber","" },
 			{ "R/RH Wheel Rim","" },
 			{ "R/LH Wheel Rim","" },
 			{ "RH Tire","" },
 			{ "LH Tire","" },
 			{ "RH Swing Arm","" },
 			{ "LH Swing Arm","" },
 			{ "Distance Piece","" },
 			{ "Silent Bush Swing Arm","" },
 			{ "R/LH Tail Light","" },
  			{ "R/RH Tail Light","" },
 			{ "Front Number Plate","" },
 			{ "Rear Number Plate","" },
 			{ "Silencer","" },
 			{ "Accelerator cable","" },
 			{ "Front wheel bracket","" },
 			{ "Break Oil tank","" },
 			{ "Hand break cable","" },
 			{ "Back rest support","" },
 			{ "Drive shaft","" },
 			{ "Mudguard beading","" },
 			{ "Front cabin","" },
 			{ "Rear cabin","" },
 			{ "s/s modifications","" },
 			{ "Sun visors","" },
 			{ "Ornaments","" },
 			{ "Arial","" },
 			{ "Radio/CD/Cassette player","" }
    		},
         }
  	};
	
	FormObject fo;  
	static boolean booleanlistThreeWheel[][][][];
	static boolean booleanprelistThreeWheel[][][][];
	Button back;
	EditText otherfield;
	
	  /** Called when the activity is first created. */
	  @Override
	  public void onCreate(Bundle icicle){
		 
	        try {
	        	LOG.debug("ENTRY onCreate");
				super.onCreate(icicle);
				setContentView(R.layout.multilevel_expandable_list_main);
				fo=Application.getFormObjectInstance();
				back = (Button)findViewById(R.id.button2);
				otherfield = (EditText)findViewById(R.id.damagedItemsOtherField);
				
				if(fo.getisPreSelected())  //PRE
				{
					otherfield.setText(fo.getpredamagedItemsOtherField());
					booleanprelistThreeWheel = fo.getBooleanprelistThreeWheel();
				    colorExpListAdapter=
						new ColorExpListAdapterThreeWheel(
							this,
				            getExpandableListView(),
				            listdesc
						);
					setListAdapter( colorExpListAdapter );
					
					back.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.back_button_xml));
				}
				else {
				otherfield.setText(fo.getdamagedItemsOtherField());
				booleanlistThreeWheel = fo.getBooleanlistThreeWheel();
				colorExpListAdapter=
					new ColorExpListAdapterThreeWheel(
						this,
				        getExpandableListView(),
				        listdesc
					);
				setListAdapter( colorExpListAdapter );
				
				back.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.cancel_button_xml));
				}
				if(!fo.isEditable()){
					editDisable();
				}
				LOG.debug("SUCCESS onCreate");
			} catch (NotFoundException e) {
				 LOG.error("onCreate:10319");
				   if(e != null){
					LOG.error("Message: " + e.getMessage());
					LOG.error("StackTrace: " + e.getStackTrace());
				  }
			} catch (Exception e) {
				 LOG.error("onCreate:10320");
				   if(e != null){
					LOG.error("Message: " + e.getMessage());
					LOG.error("StackTrace: " + e.getStackTrace());
				  }
			}
	    }
	  
	  public void editDisable() {
	    	try {
				otherfield.setFocusable(false);
				
				if(fo.getisPreSelected())  //PRE
				{
					back.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.back_button_xml));
				} else{
					back.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.back_button_xml));
				}
			} catch (NotFoundException e) {
				 LOG.error("editDisable:10321");
				   if(e != null){
					LOG.error("Message: " + e.getMessage());
					LOG.error("StackTrace: " + e.getStackTrace());
				  }
			} catch (Exception e) {
				 LOG.error("editDisable:10322");
				   if(e != null){
					LOG.error("Message: " + e.getMessage());
					LOG.error("StackTrace: " + e.getStackTrace());
				  }
			}
	    }
	    
	    public void editEnable() {
	    	try {
				otherfield.setFocusable(true);
				
				if(fo.getisPreSelected())  //PRE
				{
					back.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.back_button_xml));
				} else{
					back.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.cancel_button_xml));
				}
			} catch (NotFoundException e) {
				 LOG.error("editEnable:10323");
				   if(e != null){
					LOG.error("Message: " + e.getMessage());
					LOG.error("StackTrace: " + e.getStackTrace());
				  }
			} catch (Exception e) {
				 LOG.error("editEnable:10324");
				   if(e != null){
					LOG.error("Message: " + e.getMessage());
					LOG.error("StackTrace: " + e.getStackTrace());
				  }
			}
	    }
	  
	  public boolean onKeyDown(int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				return false;
			}
			return super.onKeyDown(keyCode, event);
		}
	  
	  
	  public void multilevel_ok_click(View v){
    	  try{
    		  if(fo.getisPreSelected())  //PRE
    	        {
    			  fo.setBooleanprelistThreeWheel(booleanprelistThreeWheel);
    			  fo.setpredamagedItemsOtherField(otherfield.getText().toString().trim());
    	        }
    		  else{
    			  fo.setBooleanlistThreeWheel(booleanlistThreeWheel);
    			  fo.setdamagedItemsOtherField(otherfield.getText().toString().trim());
    		  }
      	  
      	  Intent mIntent = new Intent(ExpList3_ThreeWheel.this,
      				VehicleDetailsActivity.class);
      		mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      		startActivity(mIntent);
      		finish();
    	  }
    	  catch (Exception e) {
    		  LOG.error("multilevel_ok_click:10325");
    		   if(e != null){
    			LOG.error("Message: " + e.getMessage());
    			LOG.error("StackTrace: " + e.getStackTrace());
    		  }
		}
	  }
	  
	  public void multilevel_back_button(View v){
		  try {
			if (fo.isEditable()) {
				int p1 = Application.get4DArrSizeSec1();
				int p2 = Application.get4DArrSizeSec2();
				int p3 = Application.get4DArrSizeSec3();
				int p4 = Application.get4DArrSizeSec4();
				boolean[][][][] a = new boolean[p1][p2][p3][p4];
				  if(!fo.getisPreSelected()) {
					  //Damaged Items
					  fo.setisVehicleShow(true);
					  fo.setdamagedItemsOtherField("");
					  fo.setpredamagedItemsOtherField("");
					  fo.setBooleanlistThreeWheel(a);
					  fo.setBooleanprelistThreeWheel(a);
					}
				  else {
					  	fo.setBooleanprelistThreeWheel(a);
						fo.setpredamagedItemsOtherField(otherfield.getText().toString().trim());
					}
			}
			finish();
		} catch (Exception e) {
			 LOG.error("multilevel_back_button:10326");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	    }
	  
	  private ColorExpListAdapterThreeWheel colorExpListAdapter;
}